/*
 * Copyright (c) 2009 Sylvestre Gallon <syl@pmbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#import "md2loader.h"

@implementation md2loader 

- (id) initWithFileName: (const char *) name {
	uint32_t *dat;

	self = [super init];

	if (self) {
		if ((fd = fopen(name, "r")) == NULL) {
			LOG(LOG_ERR, "%s open failed\n", name);
			return nil;
		}

		dat = malloc(HEADER_SIZE);
		fread(dat, HEADER_SIZE, 1, fd);

		ident = dat[0];
		if (ident != MD2_MAGIC) {
			LOG(LOG_ERR, "MD2_HEADER MAGIC WRONG : %x\n", ident);
			return nil;
		}
		version = dat[1];
	
		skinwidth = dat[2];
		skinheight = dat[3];
		framesize = dat[4];

		num_skins = dat[5];
		num_vertices = dat[6];
		num_st = dat[7];
		num_trus = dat[8];
		num_glcmds = dat[9];
		num_frames = dat[10];

		offset_skins = dat[11];
		offset_st = dat[12];
		offset_tris = dat[13];
		offset_frames = dat[14];
		offset_glcmds = dat[15];
		offset_end = dat[16];

		free(dat);
	}

	return self;
}

- (uint32_t) ident {
	return ident;
}

- (uint32_t) version {
	return version;
}

- (uint32_t) skinwidth {
	return skinwidth;
}

- (uint32_t) skinheight {
	return skinheight;
}

- (uint32_t) framesize {
	return framesize;
}

- (uint32_t) num_skins {
	return num_skins;
}

- (uint32_t) num_vertices {
	return num_vertices;
}

- (uint32_t) num_st {
	return num_st;
}

- (uint32_t) num_trus {
	return num_trus;
}

- (uint32_t) num_glcmds {
	return num_glcmds;
}

- (uint32_t) num_frames {
	return num_frames;
}

- (uint32_t) offset_skins {
	return offset_skins;
}

- (uint32_t) offset_st {
	return offset_st;
}

- (uint32_t) offset_tris {
	return offset_tris;
}

- (uint32_t) offset_frames {
	return offset_frames;
}

- (uint32_t) offset_glcmds {
	return offset_glcmds;
}

- (uint32_t) offset_end {
	return offset_end;
}

- (void) dump_info {
	char *str;

	str = malloc(5 * sizeof(*str));
	str[0] = (uint8_t) (ident & 0x000000ff);
	str[1] = (uint8_t)((ident & 0x0000ff00) >> 8);
	str[2] = (uint8_t)((ident & 0x00ff0000) >> 16);
	str[3] = (uint8_t)((ident & 0xff000000) >> 24);
	str[4] = 0;
	printf("%s\n", str);

	LOG(LOG_INFO, "MD2 HEADER\n");
	LOG(LOG_INFO, "ident: %s\n", str);
	LOG(LOG_INFO, "version:%i\n", version);
	LOG(LOG_INFO, "skinwidth:%i\n", skinwidth);
	LOG(LOG_INFO, "skinheight:%i\n", skinheight);
	LOG(LOG_INFO, "framesize:%i\n", framesize);
	LOG(LOG_INFO, "num_skins:%i\n", num_skins);
	LOG(LOG_INFO, "num_vertices:%i\n", num_vertices);
	LOG(LOG_INFO, "num_st:%i\n", num_st);
	LOG(LOG_INFO, "num_trus:%i\n", num_trus);
	LOG(LOG_INFO, "num_glcmds:%i\n", num_glcmds);
	LOG(LOG_INFO, "num_frames:%i\n", num_frames);
	LOG(LOG_INFO, "offset_skins:%i\n", offset_skins);
	LOG(LOG_INFO, "offset_st:%i\n", offset_st);
	LOG(LOG_INFO, "offset_tris:%i\n", offset_tris);
	LOG(LOG_INFO, "offset_frames:%i\n", offset_frames);
	LOG(LOG_INFO, "offset_glcmds:%i\n", offset_glcmds);
	LOG(LOG_INFO, "offset_end:%i\n", offset_end);
}

- (void) loadmodel {
	/* XXX TODO */
}

- (void) loadtexture {
	/* XXX TODO */
}

- (void) renderframe {
	/* XXX TODO */
}

- (void) setscale: (GLfloat) sc {
	scale = sc;
}

- (GLfloat) scale {
	return scale;
}

- (void) release {
	fclose(fd);
	[super release];
}

@end 
